package com.qiang.work.test07;

import java.io.*;
import java.util.UUID;

public class Copy {
    public static void main(String[] args) throws IOException {
        //创建方法
        //传入两个参数
        File f1 = new File("D:\\aa");
        File f2 = new File("D:\\bb");
        copyPic(f1, f2);
    }

    private static void copyPic(File f1, File f2) throws IOException {

        //获取f1文件列表
        File[] f3 = f1.listFiles();

        //遍历列表
        for (File f4 : f3) {

            //获取文件名字
            String name = f4.getName();
            File file = new File(f1, f4.getName());
            File file1 = new File(f2, f4.getName());

            //判断是否是文件夹
            if (f4.isDirectory()) {
                //是文件夹的话，就在f2里面创建文件夹
                File f5 = new File(f1, name);
                File f6 = new File(f2, name);
                f6.mkdirs();
                //System.out.println(f5.getPath());
                //System.out.println(f6.getPath());
                copyPic(f5, f6);
            } else {
                String sbt = name.substring(name.length() - 3, name.length());
                //判断是否是图片
                if (sbt.equals("jpg")) {
                    //创建读取流
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                    //创建写出流
                    //BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f2 + UUID.randomUUID().toString() + ".jpg"));
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file1));

                    byte[] bb = new byte[1024];
                    int len;
                    while ((len = bis.read(bb)) != -1) {
                        bos.write(bb, 0, len);
                    }
                    bis.close();
                    bos.close();
                }
            }
        }
    }
}
